/*
 * ShopCategoryGoodsFragment.java
 * business
 *
 * Created by ChenTao on 2016/10/25.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail.categorygoods;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseEventFragment;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.GoodsGridProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.GoodsListProvider;
import com.yidiandao.business.ui.widget.view.SortMenu;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;

/** 店铺分类详情-View */
public class ShopCategoryGoodsFragment extends SuperBaseEventFragment
        implements ShopCategoryGoodsContract.View {

    private ShopCategoryGoodsContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private TextView mTvTitle;

    private SortMenu mSortMenu;

    private RecyclerView mRecyclerView;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private MultiTypeLoadMoreAdapter mGridAdapter;

    private MultiTypeLoadMoreAdapter mListAdapter;

    private LoadingDialog mLoadingDialog;

    public static ShopCategoryGoodsFragment getInstance(Bundle bundle) {
        ShopCategoryGoodsFragment fragment = new ShopCategoryGoodsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setPresenter(ShopCategoryGoodsContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ShopCategoryGoodsFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTvTitle = (TextView) mRootView.findViewById(R.id.tv_title);
        mSortMenu = (SortMenu) mRootView.findViewById(R.id.sort_menu);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mLoadMoreContainer =
                (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);

        mPresenter.extraBundle(getArguments());
        initRecyclerView();
        initListeners();
    }

    @Override
    protected void startLoadData() {
        mPresenter.loadCategoryNewGoodsList(true);
    }

    private void initRecyclerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mGridAdapter = new MultiTypeLoadMoreAdapter();
        mListAdapter = new MultiTypeLoadMoreAdapter();

        mGridAdapter.register(DoubleGoods.class, new GoodsGridProvider(getActivity()));
        mListAdapter.register(Goods.class, new GoodsListProvider(getActivity()));

        // 初始化为GridStyle
        changeAdapterStyle(false);
    }

    private void changeAdapterStyle(boolean isGridStyle) {
        // 当前是否是网格状布局
        mLoadMoreContainer.setRecyclerViewAdapter(isGridStyle ? mListAdapter : mGridAdapter);
        mLoadMoreContainer.useDefaultFooter();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });

        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        // 上拉加载
                        switch (mSortMenu.getCurrentMenu()) {
                            case SortMenu.MENU_NEWEST:
                                mPresenter.loadCategoryNewGoodsList(false);
                                break;
                            case SortMenu.MENU_PRICE:
                                mPresenter.loadCategoryPriceGoodsList(false, mSortMenu.isSortUp());
                                break;
                            case SortMenu.MENU_SALES:
                                mPresenter.loadCategorySalesGoodsList(false);
                                break;
                        }
                    }
                });

        mSortMenu.setOnSortMenuClickListener(
                new SortMenu.OnSortMenuClickListener() {
                    @Override
                    public void onSortMenuClick(int menu, boolean isSortUp) {
                        switch (menu) {
                            case SortMenu.MENU_NEWEST:
                                mPresenter.loadCategoryNewGoodsList(true);
                                break;
                            case SortMenu.MENU_PRICE:
                                mPresenter.loadCategoryPriceGoodsList(true, isSortUp);
                                break;
                            case SortMenu.MENU_SALES:
                                mPresenter.loadCategorySalesGoodsList(true);
                                break;
                        }
                    }

                    @Override
                    public void onChangeListStyle(boolean isGridStyle) {
                        changeAdapterStyle(isGridStyle);
                        mPresenter.changeStyle(isGridStyle);
                    }
                });
    }

    @Override
    public void setTitle(String categoryName) {
        mTvTitle.setText(categoryName);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop_category_goods;
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void showLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(mActivity);
        }
        mLoadingDialog.show();
    }

    @Override
    public void hideLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void setGoodsGrid(List<DoubleGoods> goodsGridList) {
        mGridAdapter.setItems(goodsGridList);
        mGridAdapter.notifyDataSetChanged();
    }

    @Override
    public void setGoodsList(List<Goods> goodsList) {
        mListAdapter.setItems(goodsList);
        mListAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public void notifyDataChanged() {
        startLoadData();
    }
}
